package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.ProjectType;
import com.dingreading.cloud.admin.entity.table.ProjectTypeTableDef;
import com.dingreading.cloud.admin.mapper.ProjectTypeMapper;
import com.dingreading.cloud.admin.service.ProjectTypeService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class ProjectTypeServiceImpl extends BaseServiceImpl<ProjectTypeMapper, ProjectType, ProjectTypeTableDef> implements ProjectTypeService {

    @Override
    protected ProjectTypeTableDef getTable() {
        return ProjectTypeTableDef.projectType;
    }

    @Override
    public Page<ProjectType> pageList(PageUtil pageUtil, String keyword) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.name.like(keyword));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.source.asc(), table.id.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public long getSameNameCnt(Long id, String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (id != null)
            condition.and(table.id.ne(id));
        condition.and(table.name.like(name));

        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public List<ProjectType> getList(String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(name))
            condition.and(table.name.like(name));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());

        return getMapper().selectListByQuery(wrapper);
    }

}
